var doctorHtml;
var windowNameIndex = parent.layer.getFrameIndex(window.name); //获取窗口索引
$(function () {
    var status = $("#dealStatus").val();
    if (status == "已结账") {
        layer.msg('已结账的开单记录无法进行修改操作', {time: 800, icon: 2});
        setTimeout(function () {
            $("#relaodBtn", parent.document).click();
            parent.layer.close(windowNameIndex);
        }, 1200);
    }
    getTreatmentUnit();
});

//获取科室
function getTreatmentUnit() {
    var treatmentUnit = $("#treatmentUnit").val();
    if (treatmentUnit.indexOf(',') < 0) {
        var html = '<a class="btn treatmentUnitName" onclick="getFirstLevelProjectByTreatmentUnit(\'' + treatmentUnit + '\')">' + treatmentUnit + '</a>';
        $("#treatmentUnitHtml").html(html);
        getFirstLevelProjectByTreatmentUnit(treatmentUnit);
    } else {
        var treatmentUnits = [];
        treatmentUnits = treatmentUnit.split(',');
        var html = '';
        for (var i = 0; i < treatmentUnits.length; i++) {
            html += '<a class="btn treatmentUnitName" onclick="getFirstLevelProjectByTreatmentUnit(\'' + treatmentUnits[i] + '\')">' + treatmentUnits[i] + '</a>';
        }
        $("#treatmentUnitHtml").html(html);
        getFirstLevelProjectByTreatmentUnit(treatmentUnits[0]);
    }
}

//根据科室获取一级项目
function getFirstLevelProjectByTreatmentUnit(treatmentUnit) {
    var customerId = $('#customerId').val();
    $("#treatmentUnit").val(treatmentUnit);
    var dc = treatmentUnit + "," + customerId;
    $.ajax({
        url: Feng.ctxPath + "/payService/getFirstLevelProject",
        type: "post",
        data: {"dc": dc},
        success: function (data) {
            var txt = '';
            for (var i = 0; i < data.length; i++) {
                txt += ' <option value="' + data[i].firstLevelProject + '"> ' + ' &nbsp' + data[i].firstLevelProject + '</option>';
            }
            document.getElementById('firstLevelProjectDivsd').innerHTML = txt;
            openFirstLevelProject();
        },
        error: function (data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(windowNameIndex);
        }
    });
}

//根据一级项目获取收费项目
function openFirstLevelProject() {
    var department = $("#treatmentUnit").val();
    var customerId = $('#customerId').val();
    var firstLevelProject = $("#firstLevelProjectDivsd").val();
    var cdf = customerId + "," + department + "," + firstLevelProject;
    $.ajax({
        url: Feng.ctxPath + "/payService/getPaySeriveName",
        type: "post",
        data: {"cdf": cdf},
        success: function (data1) {
            var txts = '';
            for (var i = 0; i < data1.length; i++) {
                // if (data1[i].payServicePriceOne == data1[i].payServicePriceTwo) {
                txts += ' <div style="margin: 10px 0"> ' +
                    ' <a href=\"javascript:;\" style="font-size: 14px;" onclick=\"chooseProject(\'' + data1[i].id + '\')\">&nbsp' + data1
                        [i].payServiceName + '&nbsp' + data1[i].payServicePriceTwo + '元</a> ' +
                    ' </div>';
                // } else {
                //     txts += ' <div style="margin: 10px 0"> ' +
                //         ' <a href=\"javascript:;\" style="font-size: 14px;" onclick=\"chooseProject(\'' + data1[i].id + '\')\">&nbsp' + data1
                //             [i].payServiceName + '&nbsp' + data1[i].payServicePriceOne + '-' + data1[i].payServicePriceTwo + '元</a> ' +
                //         ' </div>';
                // }
            }
            document.getElementById('payServiceNamesd').innerHTML = txts;
        },
        error: function (data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(windowNameIndex);
        }
    });
}

//选择开单项目
function chooseProject(id) {
    var member = $("#discountDiscount").val();
    $.ajax({
        url: Feng.ctxPath + "/consultant/getCustomerStatus/" + $("#customerId").val() ,
        dataType: "json",
        contentType: "application/json",
        type: "POST",
        success: function (data) {
            if (data.split(",")[0] == '复诊') {
                alert("复诊不能重新开单,请去进行重咨操作！");
                return
            } else if (data.split(",")[2] == 3) {
                alert("患者TC不是当前登录人，请联系前台分配患者！");
                return
            } else {
                $.ajax({
                    type: "POST",
                    url: Feng.ctxPath + "/payService/projectDetail",
                    data: {
                        id: id
                    },
                    dataType: "json",
                    success: function (data) {
                        var dealList = $(".proDealInfo");
                        var isChoose = 0;
                        for (var i = 0; i < dealList.length; i++) {
                            if (data.id == dealList[i].payServiceId) {
                                isChoose = 1;
                            }
                        }
                        if (isChoose == 0) {
                            if (data.firstLevelProject == '种植牙俱乐部') {
                                var html = '<div class="proDealInfo"><div id="' + data.id + 'toothBox" class="toothBox" style="display: flex;flex-direction: column;flex: 1;height: 24px;"><div style="flex: 1;display: flex;border-bottom: 1px solid #999999"><div id="' + data.id + 'toothBox1" class="leftUp" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox2" class="rightUp" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div><div style="flex: 1;display: flex"><div id="' + data.id + 'toothBox3" class="leftDown" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox4" class="rightDown" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div></div><div style="flex: 2;" class="beyond" title="' + data.payServiceName + '"><span class="payServiceName">' + data.payServiceName + '<span><input class="payServiceId" value="' + data.id + '" type="hidden" /><input class="vipPayserviceId" value="0" type="hidden" /></div><div><input autocomplete="off" disabled="disabled" oninput="decimalNum(this),updatePrice(this)" type="text" class="payService" name="payService" value="' + data.payServicePriceTwo + '"/><input class="discountHide" value="' + data.payServicePriceTwo + '" type="hidden"><input class="unitPrice" value="' + data.payServicePriceTwo + '" type="hidden"><input class="unitPriceOne" value="' + data.payServicePriceOne + '" type="hidden"/><input class="unitPriceTwo" value="' + data.payServicePriceTwo + '" type="hidden"/><input class="priceRecord" value="' + data.payServicePriceTwo + '" type="hidden"/><input class="priceDiscountRecord" value="100" type="hidden"/><input class="primary" value="' + data.payServicePriceTwo + '" type="hidden"></div><div><span>' + data.unit + '</span></div><div><a onclick="numReduce(this)" style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="reduce">-</a><input class="num" oninput="decimalNum(this),changeNum(this)" style="width:35px;vertical-align: top;" type="text" value="1"><a onclick="numAdd(this)" style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="add">+</a></div><div><input autocomplete="off" disabled="disabled" oninput="decimalNum(this),priceDiscount(this)" class="amount" value="' + data.payServicePriceTwo + '" type="text"/><input class="realAmount" value="' + payServicePriceTwo + '" type="hidden"/></div><div><input disabled="disabled" autocomplete="off" oninput="decimalNum(this),updateDiscount(this)" class="discount" value="100" type="text"/></div><div><select onchange="updateDoctor(this)" class="doctor" style="width: 90%;">' + doctorHtml + '</select></div><div><a class="deleteDeal" onclick="deleteProject(this)">删除</a></div></div>';
                                $("#projectListInfo").append(html);
                            } else {
                                if (data.remarks == "a") {
                                    var discount = 100 * member;
                                    var payServicePriceTwo = parseFloat(data.payServicePriceTwo).toFixed(1);
                                    var payServicePriceT = parseFloat(payServicePriceTwo / member).toFixed(1);
                                    var html = '<div class="proDealInfo"><div id="' + data.id + 'toothBox" class="toothBox" style="display: flex;flex-direction: column;flex: 1;height: 24px;"><div style="flex: 1;display: flex;border-bottom: 1px solid #999999"><div id="' + data.id + 'toothBox1" class="leftUp" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox2" class="rightUp" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div><div style="flex: 1;display: flex"><div id="' + data.id + 'toothBox3" class="leftDown" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox4" class="rightDown" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div></div><div style="flex: 2;" class="beyond" title="' + data.payServiceName + '"><span class="payServiceName">' + data.payServiceName + '<span><input class="payServiceId" value="' + data.id + '" type="hidden" /><input class="vipPayserviceId" value="0" type="hidden" /></div><div><input autocomplete="off" oninput="decimalNum(this),updatePrice(this)" type="text" class="payService" name="payService" value="' + payServicePriceT + '"/><input class="discountHide" value="' + payServicePriceT + '" type="hidden"><input class="unitPrice" value="' + data.payServicePriceTwo + '" type="hidden"><input class="unitPriceOne" value="' + data.payServicePriceOne + '" type="hidden"/><input class="unitPriceTwo" value="' + payServicePriceT + '" type="hidden"/><input class="priceRecord" value="' + payServicePriceT + '" type="hidden"/><input class="priceDiscountRecord" value="' + discount + '" type="hidden"/><input class="primary" value="' + payServicePriceTwo + '" type="hidden"></div><div><span>' + data.unit + '</span></div><div><a onclick="numReduce(this)" style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="reduce">-</a><input class="num" oninput="decimalNum(this),changeNum(this)" style="width:35px;vertical-align: top;" type="text" value="1"><a onclick="numAdd(this)" style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="add">+</a></div><div><input autocomplete="off" oninput="decimalNum(this),priceDiscount(this)" class="amount" value="' + payServicePriceTwo + '" type="text"/><input class="realAmount" value="' + payServicePriceTwo + '" type="hidden"/></div><div><input autocomplete="off" oninput="decimalNum(this),updateDiscount(this)" class="discount" value="' + discount + '" type="text"/></div><div><select onchange="updateDoctor(this)" class="doctor" style="width: 90%;">' + doctorHtml + '</select></div><div><a class="deleteDeal" onclick="deleteProject(this)">删除</a></div></div>';
                                    $("#projectListInfo").append(html);
                                } else {
                                    var discount = 100 * member;
                                    var payServicePriceTwo = parseFloat(data.payServicePriceTwo * member).toFixed(1);
                                    var html = '<div class="proDealInfo"><div id="' + data.id + 'toothBox" class="toothBox" style="display: flex;flex-direction: column;flex: 1;height: 24px;"><div style="flex: 1;display: flex;border-bottom: 1px solid #999999"><div id="' + data.id + 'toothBox1" class="leftUp" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox2" class="rightUp" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div><div style="flex: 1;display: flex"><div id="' + data.id + 'toothBox3" class="leftDown" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox4" class="rightDown" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div></div><div style="flex: 2;" class="beyond" title="' + data.payServiceName + '"><span class="payServiceName">' + data.payServiceName + '<span><input class="payServiceId" value="' + data.id + '" type="hidden" /><input class="vipPayserviceId" value="0" type="hidden" /></div><div><input autocomplete="off" oninput="decimalNum(this),updatePrice(this)" type="text" class="payService" name="payService" value="' + data.payServicePriceTwo + '"/><input class="discountHide" value="' + data.payServicePriceTwo + '" type="hidden"><input class="unitPrice" value="' + data.payServicePriceTwo + '" type="hidden"><input class="unitPriceOne" value="' + data.payServicePriceOne + '" type="hidden"/><input class="unitPriceTwo" value="' + data.payServicePriceTwo + '" type="hidden"/><input class="priceRecord" value="' + data.payServicePriceTwo + '" type="hidden"/><input class="priceDiscountRecord" value="' + discount + '" type="hidden"/><input class="primary" value="' + payServicePriceTwo + '" type="hidden"></div><div><span>' + data.unit + '</span></div><div><a onclick="numReduce(this)" style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="reduce">-</a><input class="num" oninput="decimalNum(this),changeNum(this)" style="width:35px;vertical-align: top;" type="text" value="1"><a onclick="numAdd(this)" style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="add">+</a></div><div><input autocomplete="off" oninput="decimalNum(this),priceDiscount(this)" class="amount" value="' + payServicePriceTwo + '" type="text"/><input class="realAmount" value="' + payServicePriceTwo + '" type="hidden"/></div><div><input autocomplete="off" oninput="decimalNum(this),updateDiscount(this)" class="discount" value="' + discount + '" type="text"/></div><div><select onchange="updateDoctor(this)" class="doctor" style="width: 90%;">' + doctorHtml + '</select></div><div><a class="deleteDeal" onclick="deleteProject(this)">删除</a></div></div>';
                                    $("#projectListInfo").append(html);
                                }
                            }
                            totalAmountMoney();
                        } else {
                            layer.msg('请不要选择重复的收费项目！', {
                                time: 1000,
                                icon: 2
                            })
                        }
                    }
                })
            }
        }
    })
}

function chooseProjectTwo(id, vipPayserviceId, obj) {
    var member = $("#discountDiscount").val();
    $.ajax({
        url: Feng.ctxPath + "/consultant/getCustomerStatus/" + $("#customerId").val() ,
        dataType: "json",
        contentType: "application/json",
        type: "POST",
        success: function (data) {
            if (data.split(",")[0] == '复诊') {
                alert("复诊不能重新开单,请去进行重咨操作！");
                return
            } else if (data.split(",")[2] == 3) {
                alert("患者TC不是当前登录人，请联系前台分配患者！");
                return
            } else {
                $.ajax({
                    type: "POST",
                    url: Feng.ctxPath + "/payService/projectDetail",
                    data: {
                        id: id
                    },
                    dataType: "json",
                    success: function (data) {
                        $(obj).parent().css({'pointer-events': 'none', 'background': '#F1F1F1'});
                        var dealList = $(".proDealInfo");
                        var isChoose = 0;
                        for (var i = 0; i < dealList.length; i++) {
                            if (data.id == dealList[i].payServiceId) {
                                isChoose = 1;
                            }
                        }
                        if (isChoose == 0) {
                            if (data.remarks == "a") {
                                var discount = 100 * member;
                                var payServicePriceTwo = parseFloat(data.payServicePriceTwo).toFixed(1);
                                var payServicePriceT = parseFloat(payServicePriceTwo / member).toFixed(1);
                                var html = '<div class="proDealInfo"><div id="' + data.id + 'toothBox" class="toothBox" style="display: flex;flex-direction: column;flex: 1;height: 24px;"><div style="flex: 1;display: flex;border-bottom: 1px solid #999999"><div id="' + data.id + 'toothBox1" class="leftUp" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox2" class="rightUp" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div><div style="flex: 1;display: flex"><div id="' + data.id + 'toothBox3" class="leftDown" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox4" class="rightDown" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div></div><div style="flex: 2;" class="beyond" title="' + data.payServiceName + '"><span class="payServiceName">' + data.payServiceName + '<span><input class="payServiceId" value="' + data.id + '" type="hidden" /><input class="vipPayserviceId" value="' + vipPayserviceId + '" type="hidden" /></div><div><input readonly="readonly" oninput="decimalNum(this),updatePrice(this)" type="text" class="payService" name="payService" value="0"/><input class="discountHide" value="0" type="hidden"><input class="unitPrice" value="0" type="hidden"><input class="unitPriceOne" value="0" type="hidden"/><input class="unitPriceTwo" value="0" type="hidden"/><input class="priceRecord" value="0" type="hidden"/><input class="priceDiscountRecord" value="100" type="hidden"/><input class="primary" value="0" type="hidden"></div><div><span>' + data.unit + '</span></div><div><a style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="reduce">-</a><input readonly="readonly" class="num" oninput="decimalNum(this),changeNum(this)" style="width:35px;vertical-align: top;" type="text" value="1"><a style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="add">+</a></div><div><input readonly="readonly" oninput="decimalNum(this),priceDiscount(this)" class="amount" value="0" type="text"/><input class="realAmount" value="0" type="hidden"/></div><div><input oninput="decimalNum(this),updateDiscount(this)" class="discount" value="100" type="text"/></div><div><select onchange="updateDoctor(this)" class="doctor" style="width: 90%;">' + doctorHtml + '</select></div><div><a class="deleteDeal" onclick="deleteProject(this,\'' + vipPayserviceId + '\')">删除</a></div></div>';
                                $("#projectListInfo").append(html);
                            } else {
                                var discount = 100 * member;
                                var payServicePriceTwo = parseFloat(data.payServicePriceTwo * member).toFixed(1);
                                var html = '<div class="proDealInfo"><div id="' + data.id + 'toothBox" class="toothBox" style="display: flex;flex-direction: column;flex: 1;height: 24px;"><div style="flex: 1;display: flex;border-bottom: 1px solid #999999"><div id="' + data.id + 'toothBox1" class="leftUp" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox2" class="rightUp" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div><div style="flex: 1;display: flex"><div id="' + data.id + 'toothBox3" class="leftDown" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox4" class="rightDown" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div></div><div style="flex: 2;" class="beyond" title="' + data.payServiceName + '"><span class="payServiceName">' + data.payServiceName + '<span><input class="payServiceId" value="' + data.id + '" type="hidden" /><input class="vipPayserviceId" value="' + vipPayserviceId + '" type="hidden" /></div><div><input readonly="readonly" oninput="decimalNum(this),updatePrice(this)" type="text" class="payService" name="payService" value="0"/><input class="discountHide" value="0" type="hidden"><input class="unitPrice" value="0" type="hidden"><input class="unitPriceOne" value="0" type="hidden"/><input class="unitPriceTwo" value="0" type="hidden"/><input class="priceRecord" value="0" type="hidden"/><input class="priceDiscountRecord" value="100" type="hidden"/><input class="primary" value="0" type="hidden"></div><div><span>' + data.unit + '</span></div><div><a style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="reduce">-</a><input readonly="readonly" class="num" oninput="decimalNum(this),changeNum(this)" style="width:35px;vertical-align: top;" type="text" value="1"><a style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="add">+</a></div><div><input readonly="readonly" oninput="decimalNum(this),priceDiscount(this)" class="amount" value="0" type="text"/><input class="realAmount" value="0" type="hidden"/></div><div><input oninput="decimalNum(this),updateDiscount(this)" class="discount" value="100" type="text"/></div><div><select onchange="updateDoctor(this)" class="doctor" style="width: 90%;">' + doctorHtml + '</select></div><div><a class="deleteDeal" onclick="deleteProject(this,\'' + vipPayserviceId + '\')">删除</a></div></div>';
                                $("#projectListInfo").append(html);
                            }
                            totalAmountMoney();
                        } else {
                            layer.msg('请不要选择重复的收费项目！', {
                                time: 1000,
                                icon: 2
                            })
                        }
                    }
                })
            }
        }
    })
}

function chooseDoctor() {
    var proDoctor = $("#projectDoctor").val();
    $(".doctor").val(proDoctor);
}

function decimalNum(obj) {
    obj.value = obj.value.replace(/[^\d.]/g, "");
    obj.value = obj.value.replace(/^\./g, "");
    obj.value = obj.value.replace(/\.{2,}/g, ".");
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
    if (obj.value.indexOf(".") < 0 && obj.value != "") {
        obj.value = parseFloat(obj.value)
    }
}

function num(obj) {
    obj.value = obj.value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
    obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字
    obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
}

function deleteProject(obj, id) {
    $("#" + id).parent().css({'pointer-events': 'auto', 'background': 'white'});
    var project = $(obj).parent().parent();
    $(project).remove();
    var proDealList = $(".proDealInfo");
    if (proDealList.length == 0) {
        $("#totalSum1").val(0);
        $("#realTotalSum").val(0);
        $("#originalPrice").val(0);
        $("#newTotalSum").val(0);
        $("#newPayAmount").val(0);
        $("#totalDiscount").val(100);
    } else {
        totalAmountMoney();
    }
}

//修改单价
function updatePrice(obj) {
    var price = parseFloat($(obj).val()); // 当前单价
    var unitPrice = parseFloat($(obj).parent().find(".unitPrice").val()); //原价
    var discount = parseFloat($(obj).parent().parent().find(".discount").val()) / 100; //当前折扣
    var priceOne = $(obj).parent().find(".unitPriceOne").val(); //最小值
    var unitPriceTwo = $(obj).parent().find(".unitPriceTwo").val(); //最大值
    var num = parseInt($(obj).parent().parent().find(".num").val()); // 数量
    if (priceOne == unitPrice) {
        $(obj).val(unitPrice)
    } //如果没有区间 就不能更改
    else {
        if (isNaN(price)) {
            $(obj).val(0);
            $(obj).parent().parent().find(".amount").val(0);
            $(obj).parent().parent().find(".realAmount").val(0);
            totalAmountMoney();
        } else {
            if (price <= unitPriceTwo && price >= 0) {
                if (num > 0) {
                    $(obj).parent().parent().find(".priceRecord").val(price);
                    var amount = parseFloat(price * num * discount).toFixed(1); //单个项目总折后
                    $(obj).parent().parent().find(".priceDiscountRecord").val(discount);
                    $(obj).parent().parent().find(".amount").val(amount);
                    $(obj).parent().parent().find(".realAmount").val(amount);
                    $(obj).parent().parent().find(".primary").val(amount);
                }
                totalAmountMoney();
            } else {
                $(obj).val($(obj).parent().parent().find(".priceRecord").val());
            }
        }
    }
}

//修改折后价
function priceDiscount(obj) {
    var amount = parseFloat($(obj).val()); //当前折后价
    var payService = $(obj).parent().parent().find(".payService").val(); //当前单价
    var num = $(obj).parent().parent().find(".num").val(); //数量
    if (isNaN(amount)) {
        $(obj).val(0);
        $(obj).parent().parent().find(".discount").val(0);
        totalAmountMoney()
    } else {
        if (amount > (num * payService)) {
            $(obj).val($(obj).parent().parent().find(".primary").val());
        } else {
            $(obj).parent().parent().find(".primary").val(amount);
            $(obj).parent().parent().find(".priceDiscountRecord").val(discount);
            $(obj).parent().parent().find(".realAmount").val(amount);
            var discount = parseFloat(amount / (num * payService));
            $(obj).parent().parent().find(".discount").val(parseFloat(discount * 100).toFixed(2));
            totalAmountMoney();
        }
    }
}

//改变数量
function changeNum(obj) {
    var num = parseInt($(obj).val()); //数量
    var priceRecord = parseFloat($(obj).parent().parent().find(".priceRecord").val());
    var discount = parseFloat($(obj).parent().parent().find(".discount").val()) / 100; //当前折扣
    if (isNaN(num)) {
        $(obj).val(0);
        $(obj).parent().parent().find(".amount").val(0);
        $(obj).parent().parent().find(".realAmount").val(0);
        totalAmountMoney();
    } else {
        var amount = parseFloat(priceRecord * num * discount).toFixed(1);
        $(obj).parent().parent().find(".amount").val(amount);
        $(obj).parent().parent().find(".realAmount").val(amount);
        $(obj).parent().parent().find(".priceDiscountRecord").val(discount);
        var payService = parseFloat($(obj).parent().parent().find(".payService").val());
        $(obj).parent().parent().find(".primary").val(amount);
    }
    totalAmountMoney();
}

//数量减少
function numReduce(obj) {
    var parentNode = $(obj).parent();
    var num = $(parentNode).parent().find(".num").val(); //数量
    var priceRecord = $(obj).parent().parent().find(".priceRecord").val();
    var discount = parseFloat($(obj).parent().parent().find(".discount").val()) / 100; //当前折扣
    if (parseInt(num) > 1) {
        $(parentNode).parent().find(".num").val(parseInt(num) - 1);
        var count = parseInt(num) - 1;
        var amount = parseFloat(priceRecord * count * discount).toFixed(1);
        $(obj).parent().parent().find(".priceDiscountRecord").val(discount);
        $(parentNode).parent().find(".amount").val(amount);
        $(parentNode).parent().find(".realAmount").val(amount);
        $(parentNode).parent().find(".primary").val(amount);
        totalAmountMoney();
    }
}

//数量增加
function numAdd(obj) {
    var parentNode = $(obj).parent();
    var num = $(parentNode).parent().find(".num").val(); //数量
    var discountHide = $(parentNode).parent().find(".discountHide").val();
    var unitPrice = $(parentNode).parent().find(".payService").val();
    var priceRecord = $(obj).parent().parent().find(".priceRecord").val();
    var discount = parseFloat($(obj).parent().parent().find(".discount").val()) / 100; //当前折扣
    $(parentNode).parent().find(".num").val(parseInt(num) + 1);
    var count = parseInt(num) + 1;
    var amount = parseFloat(priceRecord * count * discount).toFixed(1);
    $(parentNode).parent().find(".amount").val(amount);
    $(obj).parent().parent().find(".priceDiscountRecord").val(discount);
    $(parentNode).parent().find(".realAmount").val(amount);
    $(parentNode).parent().find(".primary").val(amount);
    totalAmountMoney();
}

//修改折扣
function updateDiscount(obj) {
    var discount = parseFloat($(obj).val()) / 100; //当前折扣
    var payService = $(obj).parent().parent().find(".payService").val(); //当前单价
    if (isNaN(discount)) {
        $(obj).val(0);
        $(obj).parent().parent().find(".amount").val(0); // 折后应收
        $(obj).parent().parent().find(".realAmount").val(0); // 折后应收
        totalAmountMoney();
    } else {
        if ($(obj).val() > 100) {
            $(obj).val($(obj).parent().parent().find(".priceDiscountRecord").val());
            totalAmountMoney();
        } else {
            $(obj).parent().parent().find(".priceDiscountRecord").val($(obj).val());
            if (discount >= 0) {
                var num = $(obj).parent().parent().find(".num").val(); //数量
                var price = parseFloat(payService); //修改折扣后的单价
                $(obj).parent().parent().find(".discountHide").val(price);
                var amount = parseFloat(num * price * discount).toFixed(1);
                $(obj).parent().parent().find(".amount").val(amount); // 折后应收
                $(obj).parent().parent().find(".realAmount").val(amount); // 折后应收
                $(obj).parent().parent().find(".primary").val(amount); // 折后价
                totalAmountMoney();
            }
        }
    }
}

function totalAmountMoney() {
    var totalSum = 0;
    $("#projectListInfo .priceRecord").each(function (index, obj) {
        totalSum += parseFloat($(obj).val() * $(obj).parent().parent().find(".num").val());
    });
    var whole = 0;
    $("#projectListInfo .primary").each(function (index, obj) {
        whole += parseFloat($(obj).val());
    });
    $("#totalSum1").val(whole.toFixed(1));
    $("#originalPrice").val(totalSum.toFixed(1));
    $("#newTotalSum").val(whole.toFixed(1));
    $("#newPayAmount").val(whole.toFixed(1));
    var wholeDiscount = parseFloat(whole / totalSum).toFixed(2);
    if (isNaN(wholeDiscount)) {
        $("#totalDiscount").val(100.00);
    } else {
        $("#totalDiscount").val(wholeDiscount * 100);
    }
    $("#newArrears").val(parseFloat($("#newTotalSum").val()) - parseFloat($("#newPayAmount").val()));
}

//修改订单项目
function updateDealInfo() {
    var customerId = $('#customerId').val();
    var reservationsId = $("#reservationsId").val();
    var reservationsIdx = $('#reservationsIdx').val();
    var operationTime = $("#operationTime").val();
    var lowestPriceBill = $("#lowestPriceBill").val();
    var remarks = $("#proRemark").val();
    var intamount = 0;
    $("#projectListInfo .amount").each(function (index, obj) {
        if (parseFloat($(obj).parent().parent().find(".unitPriceOne").val()) != parseFloat($(obj).parent().parent().find(".unitPriceTwo").val())) {
            if (parseFloat($(obj).val()) < parseFloat($(obj).parent().parent().find(".unitPriceOne").val() * $(obj).parent().parent().find(".num").val())) {
                intamount++;
            }
        }
    });
    if ($('#integral').is(':checked') || $('#earnest').is(':checked')) {
        var dataList = $(".deductionDealList");
        var billList = [];
        if ($('#integral').is(':checked') && $('#earnest').is(':checked')) {
            var newTotalSum = $("#newTotalSum").val();
            var newPayAmount = $("#newPayAmount").val();
            var jfAmount = 0;
            var earnestAmount = 0;
            var jfBalance = parseFloat($("#jfbalance").html());
            var moneyBalance = parseFloat($("#moneyBalance").html());
            if (newTotalSum != newPayAmount) {
                for (var i = 0; i < dataList.length; i++) {
                    var obj = dataList[i];
                    var dealInfo = {};
                    dealInfo.reservationsIdx = reservationsIdx;
                    var treatmentUnit = $("#treatmentUnit").val();
                    dealInfo.customerId = customerId;
                    dealInfo.reservationsId = reservationsId;
                    dealInfo.operationTime = operationTime;
                    dealInfo.remarks = remarks;
                    dealInfo.doctor = doctor;
                    dealInfo.payServiceId = $(obj).find(".payServiceId").val();
                    dealInfo.vipPayserviceId = $(obj).find(".vipPayserviceId").val();
                    dealInfo.payServiceNum = parseFloat($(obj).find(".num").html());
                    var payServiceNum = parseFloat($(obj).find(".num").html());
                    var doctor = $(obj).find(".doctor").val();
                    var nurse = $("#projectNurse").val();
                    if (payServiceNum < 1) {
                        layer.msg('请输入项目数量！', {time: 1000, icon: 2});
                        return false;
                    }
                    if ((doctor == null || doctor == "") && (nurse == null || nurse == "")) {
                        layer.msg('请选择医生或护士！', {time: 1000, icon: 2});
                        return false;
                    }
                    if ("洁牙" == treatmentUnit) {
                        dealInfo.dentalHygienist = $(obj).find(".doctor").val();
                    } else {
                        dealInfo.dentalHygienist = "";
                    }
                    dealInfo.id = $(obj).find(".id").val();
                    dealInfo.foldingPay = parseFloat($(obj).find(".amount").html());
                    dealInfo.doctor = $(obj).find(".doctor").val();
                    dealInfo.nurse = $("#projectNurse").val();
                    dealInfo.leftUp = $(obj).find(".leftUp").val();
                    dealInfo.leftDown = $(obj).find(".leftDown").val();
                    dealInfo.rightUp = $(obj).find(".rightUp").val();
                    dealInfo.rightDown = $(obj).find(".rightDown").val();
                    dealInfo.deductionType = parseFloat($(obj).find(".deductionType").val());
                    var deductionType = $(obj).find(".deductionType").find("option:selected").text();
                    if (deductionType.indexOf("返现") >= 0) {
                        jfAmount = jfAmount + parseFloat($(obj).find(".deductionAmount").val());
                    }
                    earnestAmount = earnestAmount + parseFloat($(obj).find(".depositAmount").val());
                    dealInfo.deductionTypeName = parseFloat($(obj).find(".deductionType").html());
                    dealInfo.deductionAmount = parseFloat($(obj).find(".deductionAmount").val());
                    dealInfo.orderDiscount = parseFloat($(obj).find(".orderDiscount").val());
                    dealInfo.depositAmount = parseFloat($(obj).find(".depositAmount").val());
                    dealInfo.discountHide = parseFloat($(obj).find(".discountHide").val());
                    dealInfo.payService = parseFloat($(obj).find(".payService").html());     //未打折单价
                    dealInfo.primaryAmount = parseFloat($(obj).find(".primary").val());         //未打折总价
                    var deductionAmount = parseFloat($(obj).find(".deductionAmount").val());
                    var realAmount = parseFloat($(obj).find(".realAmount").html());
                    dealInfo.wholeDiscount = $("#totalDiscount").val();
                    var depositAmount = parseFloat($(obj).find(".depositAmount").val());
                    var amount = parseFloat($(obj).find(".amount").html())
                    var serviceActualPay = parseFloat($(obj).find(".realAmount").html());
                    if (newPayAmount >= serviceActualPay) {
                        dealInfo.serviceActualPay = parseFloat($(obj).find(".realAmount").html());
                        dealInfo.arrears = 0;
                        newPayAmount = newPayAmount - serviceActualPay;
                    } else {
                        dealInfo.serviceActualPay = newPayAmount;
                        dealInfo.arrears = serviceActualPay - newPayAmount;
                        newPayAmount = 0;
                    }
                    dealInfo.intamount = intamount;
                    dealInfo.lowestPriceBill = lowestPriceBill;
                    billList.push(dealInfo);
                }
            } else {
                for (var i = 0; i < dataList.length; i++) {
                    var obj = dataList[i];
                    var dealInfo = {};
                    dealInfo.reservationsIdx = reservationsIdx;
                    var treatmentUnit = $("#treatmentUnit").val();
                    dealInfo.customerId = customerId;
                    dealInfo.reservationsId = reservationsId;
                    dealInfo.operationTime = operationTime;
                    dealInfo.doctor = doctor;
                    dealInfo.remarks = remarks;
                    dealInfo.payServiceId = $(obj).find(".payServiceId").val();
                    dealInfo.vipPayserviceId = $(obj).find(".vipPayserviceId").val();
                    dealInfo.payServiceNum = parseFloat($(obj).find(".num").html());
                    var payServiceNum = parseFloat($(obj).find(".num").html());
                    var doctor = $(obj).find(".doctor").val();
                    var nurse = $("#projectNurse").val();
                    if (payServiceNum < 1) {
                        layer.msg('请输入项目数量！', {time: 1000, icon: 2});
                        return false;
                    }
                    if ((doctor == null || doctor == "") && (nurse == null || nurse == "")) {
                        layer.msg('请选择医生或护士！', {time: 1000, icon: 2});
                        return false;
                    }
                    if ("洁牙" == treatmentUnit) {
                        dealInfo.dentalHygienist = $(obj).find(".doctor").val();
                    } else {
                        dealInfo.dentalHygienist = "";
                    }
                    dealInfo.id = $(obj).find(".id").val();
                    dealInfo.foldingPay = parseFloat($(obj).find(".amount").html());
                    dealInfo.doctor = $(obj).find(".doctor").val();
                    dealInfo.nurse = $("#projectNurse").val();
                    dealInfo.leftUp = $(obj).find(".leftUp").val();
                    dealInfo.leftDown = $(obj).find(".leftDown").val();
                    dealInfo.rightUp = $(obj).find(".rightUp").val();
                    dealInfo.rightDown = $(obj).find(".rightDown").val();
                    dealInfo.serviceActualPay = parseFloat($(obj).find(".realAmount").html());
                    dealInfo.deductionType = parseFloat($(obj).find(".deductionType").val());
                    dealInfo.deductionTypeName = parseFloat($(obj).find(".deductionType").html());
                    dealInfo.deductionAmount = parseFloat($(obj).find(".deductionAmount").val());
                    dealInfo.orderDiscount = parseFloat($(obj).find(".orderDiscount").val());
                    dealInfo.depositAmount = parseFloat($(obj).find(".depositAmount").val());
                    dealInfo.discountHide = parseFloat($(obj).find(".discountHide").val());
                    dealInfo.payService = parseFloat($(obj).find(".payService").html());     //未打折单价
                    dealInfo.primaryAmount = parseFloat($(obj).find(".primary").val());         //未打折总价
                    var deductionAmount = parseFloat($(obj).find(".deductionAmount").val());
                    var realAmount = parseFloat($(obj).find(".realAmount").html());
                    dealInfo.wholeDiscount = $("#totalDiscount").val();
                    var depositAmount = parseFloat($(obj).find(".depositAmount").val());
                    var amount = parseFloat($(obj).find(".amount").html())
                    dealInfo.arrears = 0;
                    dealInfo.intamount = intamount;
                    dealInfo.lowestPriceBill = lowestPriceBill;
                    billList.push(dealInfo);
                }
            }
            if (earnestAmount > moneyBalance) {
                layer.alert("储值划扣金额不能超过储值余额！");
                return false;
            }
            if (jfAmount > jfBalance) {
                layer.alert("返现抵扣金额不能超过返现余额");
                return false;
            }
        } else if ($('#integral').is(':checked') && !$('#earnest').is(':checked')) {
            var totalJF = 0;
            var jfbalance = parseFloat($("#jfbalance").html());
            var totalSum1 = parseFloat($("#totalSum1").val());
            var payable = parseFloat($("#newPayAmount").val());
            if (totalSum1 != payable) {
                for (var i = 0; i < dataList.length; i++) {
                    var obj = dataList[i];
                    var dealInfo = {};
                    dealInfo.reservationsIdx = reservationsIdx;
                    var treatmentUnit = $("#treatmentUnit").val();
                    dealInfo.customerId = customerId;
                    dealInfo.reservationsId = reservationsId;
                    dealInfo.operationTime = operationTime;
                    dealInfo.remarks = remarks;
                    dealInfo.payServiceId = $(obj).find(".payServiceId").val();
                    dealInfo.vipPayserviceId = $(obj).find(".vipPayserviceId").val();
                    dealInfo.payServiceNum = parseFloat($(obj).find(".num").html());
                    var payServiceNum = parseFloat($(obj).find(".num").html());
                    var doctor = $(obj).find(".doctor").val();
                    var nurse = $("#projectNurse").val();
                    if (payServiceNum < 1) {
                        layer.msg('请输入项目数量！', {time: 1000, icon: 2});
                        return false;
                    }
                    if ((doctor == null || doctor == "") && (nurse == null || nurse == "")) {
                        layer.msg('请选择医生或护士！', {time: 1000, icon: 2});
                        return false;
                    }
                    if ("洁牙" == treatmentUnit) {
                        dealInfo.dentalHygienist = $(obj).find(".doctor").val();
                    } else {
                        dealInfo.dentalHygienist = "";
                    }
                    dealInfo.foldingPay = parseFloat($(obj).find(".amount").html());
                    dealInfo.id = $(obj).find(".id").val();
                    dealInfo.doctor = $(obj).find(".doctor").val();
                    dealInfo.nurse = $("#projectNurse").val();
                    dealInfo.leftUp = $(obj).find(".leftUp").val();
                    dealInfo.leftDown = $(obj).find(".leftDown").val();
                    dealInfo.rightUp = $(obj).find(".rightUp").val();
                    dealInfo.rightDown = $(obj).find(".rightDown").val();
                    dealInfo.serviceActualPay = parseFloat($(obj).find(".realAmount").html());
                    dealInfo.deductionType = parseFloat($(obj).find(".deductionType").val());
                    dealInfo.wholeDiscount = $("#totalDiscount").val();
                    var deductionType = $(obj).find(".deductionType").find("option:selected").text();
                    var deductionAmount = parseFloat($(obj).find(".deductionAmount").val());
                    if (deductionType.indexOf('返现') >= 0) {
                        totalJF += deductionAmount;
                    }
                    dealInfo.deductionTypeName = parseFloat($(obj).find(".deductionType").html());
                    dealInfo.deductionAmount = parseFloat($(obj).find(".deductionAmount").val());
                    dealInfo.orderDiscount = parseFloat($(obj).find(".orderDiscount").val());
                    dealInfo.depositAmount = 0;
                    dealInfo.discountHide = parseFloat($(obj).find(".discountHide").val());
                    dealInfo.payService = parseFloat($(obj).find(".payService").html());     //未打折单价
                    dealInfo.primaryAmount = parseFloat($(obj).find(".primary").val());         //未打折总价
                    var deductionAmount = parseFloat($(obj).find(".deductionAmount").val());
                    var serviceActualPay = parseFloat($(obj).find(".realAmount").html());
                    if (payable >= serviceActualPay) {
                        dealInfo.serviceActualPay = parseFloat($(obj).find(".realAmount").html());
                        dealInfo.arrears = 0;
                        payable = payable - serviceActualPay;
                    } else {
                        dealInfo.serviceActualPay = payable;
                        dealInfo.arrears = serviceActualPay - payable;
                        payable = 0;
                    }
                    dealInfo.intamount = intamount;
                    dealInfo.lowestPriceBill = lowestPriceBill;
                    billList.push(dealInfo);
                }
            } else {
                for (var i = 0; i < dataList.length; i++) {
                    var obj = dataList[i];
                    var dealInfo = {};
                    dealInfo.reservationsIdx = reservationsIdx;
                    var treatmentUnit = $("#treatmentUnit").val();
                    dealInfo.customerId = customerId;
                    dealInfo.reservationsId = reservationsId;
                    dealInfo.operationTime = operationTime;
                    dealInfo.remarks = remarks;
                    dealInfo.payServiceId = $(obj).find(".payServiceId").val();
                    dealInfo.vipPayserviceId = $(obj).find(".vipPayserviceId").val();
                    dealInfo.payServiceNum = parseFloat($(obj).find(".num").html());
                    var payServiceNum = parseFloat($(obj).find(".num").html());
                    var doctor = $(obj).find(".doctor").val();
                    var nurse = $("#projectNurse").val();
                    if (payServiceNum < 1) {
                        layer.msg('请输入项目数量！', {time: 1000, icon: 2});
                        return false;
                    }
                    if ((doctor == null || doctor == "") && (nurse == null || nurse == "")) {
                        layer.msg('请选择医生或护士！', {time: 1000, icon: 2});
                        return false;
                    }
                    if ("洁牙" == treatmentUnit) {
                        dealInfo.dentalHygienist = $(obj).find(".doctor").val();
                    } else {
                        dealInfo.dentalHygienist = "";
                    }
                    dealInfo.foldingPay = parseFloat($(obj).find(".amount").html());
                    dealInfo.id = $(obj).find(".id").val();
                    dealInfo.doctor = $(obj).find(".doctor").val();
                    dealInfo.nurse = $("#projectNurse").val();
                    dealInfo.leftUp = $(obj).find(".leftUp").val();
                    dealInfo.leftDown = $(obj).find(".leftDown").val();
                    dealInfo.rightUp = $(obj).find(".rightUp").val();
                    dealInfo.rightDown = $(obj).find(".rightDown").val();
                    dealInfo.serviceActualPay = parseFloat($(obj).find(".realAmount").html());
                    var deductionType = $(obj).find(".deductionType").find("option:selected").text();
                    if (deductionType.indexOf('返现') >= 0) {
                        totalJF += deductionAmount;
                    }
                    dealInfo.deductionType = parseFloat($(obj).find(".deductionType").val());
                    dealInfo.deductionTypeName = parseFloat($(obj).find(".deductionType").html());
                    dealInfo.deductionAmount = parseFloat($(obj).find(".deductionAmount").val());
                    dealInfo.orderDiscount = parseFloat($(obj).find(".orderDiscount").val());
                    dealInfo.depositAmount = 0;
                    dealInfo.discountHide = parseFloat($(obj).find(".discountHide").val());
                    dealInfo.payService = parseFloat($(obj).find(".payService").html());     //未打折单价
                    dealInfo.primaryAmount = parseFloat($(obj).find(".primary").val());         //未打折总价
                    dealInfo.wholeDiscount = $("#totalDiscount").val();
                    dealInfo.arrears = 0;
                    dealInfo.intamount = intamount;
                    dealInfo.lowestPriceBill = lowestPriceBill;
                    billList.push(dealInfo);
                }
            }
            if (totalJF > jfbalance) {
                layer.alert("返现抵扣金额不能超过返现余额！");
                return false;
            }
        } else if (!$('#integral').is(':checked') && $('#earnest').is(':checked')) {
            var moneyBalance = parseFloat($("#moneyBalance").html());
            var payable = parseFloat($("#newPayAmount").val());
            var realTotalSum2 = parseFloat($("#realTotalSum2").val());
            var totalDepositAmount = 0;
            if (realTotalSum2 != payable) {
                for (var i = 0; i < dataList.length; i++) {
                    var obj = dataList[i];
                    var dealInfo = {};
                    dealInfo.reservationsIdx = reservationsIdx;
                    var treatmentUnit = $("#treatmentUnit").val();
                    dealInfo.customerId = customerId;
                    dealInfo.reservationsId = reservationsId;
                    dealInfo.operationTime = operationTime;
                    dealInfo.remarks = remarks;
                    dealInfo.payServiceId = $(obj).find(".payServiceId").val();
                    dealInfo.vipPayserviceId = $(obj).find(".vipPayserviceId").val();
                    dealInfo.doctor = $(obj).find(".payServiceId").val();
                    dealInfo.payServiceNum = parseFloat($(obj).find(".num").html());
                    dealInfo.orderDiscount = parseFloat($(obj).find(".orderDiscount").val());
                    var payServiceNum = parseFloat($(obj).find(".num").html());
                    var doctor = $(obj).find(".doctor").val();
                    var nurse = $("#projectNurse").val();
                    if (payServiceNum < 1) {
                        layer.msg('请输入项目数量！', {time: 1000, icon: 2});
                        return false;
                    }
                    if ((doctor == null || doctor == "") && (nurse == null || nurse == "")) {
                        layer.msg('请选择医生或护士！', {time: 1000, icon: 2});
                        return false;
                    }
                    if ("洁牙" == treatmentUnit) {
                        dealInfo.dentalHygienist = $(obj).find(".doctor").val();
                    } else {
                        dealInfo.dentalHygienist = "";
                    }
                    dealInfo.foldingPay = parseFloat($(obj).find(".amount").html());
                    dealInfo.doctor = $(obj).find(".doctor").val();
                    dealInfo.nurse = $("#projectNurse").val();
                    dealInfo.id = $(obj).find(".id").val();
                    dealInfo.leftUp = $(obj).find(".leftUp").val();
                    dealInfo.leftDown = $(obj).find(".leftDown").val();
                    dealInfo.rightUp = $(obj).find(".rightUp").val();
                    dealInfo.rightDown = $(obj).find(".rightDown").val();
                    dealInfo.deductionType = "0";
                    dealInfo.deductionTypeName = "";
                    dealInfo.deductionAmount = 0;
                    dealInfo.discountHide = parseFloat($(obj).find(".discountHide").val());
                    dealInfo.payService = parseFloat($(obj).find(".payService").html());     //未打折单价
                    dealInfo.primaryAmount = parseFloat($(obj).find(".primary").val());         //未打折总价
                    dealInfo.wholeDiscount = $("#totalDiscount").val();
                    dealInfo.depositAmount = parseFloat($(obj).find(".depositAmount").val());
                    var serviceActualPay = parseFloat($(obj).find(".realAmount").html());
                    totalDepositAmount = totalDepositAmount + depositAmount;
                    if (payable >= serviceActualPay) {
                        dealInfo.serviceActualPay = parseFloat($(obj).find(".realAmount").html());
                        dealInfo.arrears = 0;
                        payable = payable - serviceActualPay;
                    } else {
                        dealInfo.serviceActualPay = payable;
                        dealInfo.arrears = serviceActualPay - payable;
                        payable = 0
                    }
                    dealInfo.intamount = intamount;
                    dealInfo.lowestPriceBill = lowestPriceBill;
                    billList.push(dealInfo);
                }
            } else {
                for (var i = 0; i < dataList.length; i++) {
                    var obj = dataList[i];
                    var dealInfo = {};
                    dealInfo.reservationsIdx = reservationsIdx;
                    var treatmentUnit = $("#treatmentUnit").val();
                    dealInfo.customerId = customerId;
                    dealInfo.reservationsId = reservationsId;
                    dealInfo.operationTime = operationTime;
                    dealInfo.remarks = remarks;
                    dealInfo.payServiceId = $(obj).find(".payServiceId").val();
                    dealInfo.vipPayserviceId = $(obj).find(".vipPayserviceId").val();
                    dealInfo.doctor = $(obj).find(".payServiceId").val();
                    dealInfo.payServiceNum = parseFloat($(obj).find(".num").html());
                    dealInfo.orderDiscount = parseFloat($(obj).find(".orderDiscount").val());
                    var payServiceNum = parseFloat($(obj).find(".num").html());
                    var doctor = $(obj).find(".doctor").val();
                    var nurse = $("#projectNurse").val();
                    if (payServiceNum < 1) {
                        layer.msg('请输入项目数量！', {time: 1000, icon: 2});
                        return false;
                    }
                    if ((doctor == null || doctor == "") && (nurse == null || nurse == "")) {
                        layer.msg('请选择医生或护士！', {time: 1000, icon: 2});
                        return false;
                    }
                    if ("洁牙" == treatmentUnit) {
                        dealInfo.dentalHygienist = $(obj).find(".doctor").val();
                    } else {
                        dealInfo.dentalHygienist = "";
                    }
                    dealInfo.foldingPay = parseFloat($(obj).find(".amount").html());
                    dealInfo.doctor = $(obj).find(".doctor").val();
                    dealInfo.nurse = $("#projectNurse").val();
                    dealInfo.id = $(obj).find(".id").val();
                    dealInfo.leftUp = $(obj).find(".leftUp").val();
                    dealInfo.leftDown = $(obj).find(".leftDown").val();
                    dealInfo.rightUp = $(obj).find(".rightUp").val();
                    dealInfo.rightDown = $(obj).find(".rightDown").val();
                    dealInfo.serviceActualPay = parseFloat($(obj).find(".realAmount").html());
                    dealInfo.deductionType = "0";
                    dealInfo.deductionTypeName = "";
                    dealInfo.deductionAmount = 0;
                    dealInfo.discountHide = parseFloat($(obj).find(".discountHide").val());
                    dealInfo.payService = parseFloat($(obj).find(".payService").html());     //未打折单价
                    dealInfo.primaryAmount = parseFloat($(obj).find(".primary").val());         //未打折总价
                    dealInfo.wholeDiscount = $("#totalDiscount").val();
                    dealInfo.depositAmount = parseFloat($(obj).find(".depositAmount").val());
                    totalDepositAmount = totalDepositAmount + depositAmount;
                    dealInfo.arrears = 0;
                    dealInfo.intamount = intamount;
                    dealInfo.lowestPriceBill = lowestPriceBill;
                    billList.push(dealInfo);
                }
            }
            if (totalDepositAmount > moneyBalance) {
                layer.alert("储值划扣金额不能超过储值余额！");
                return false;
            }
        }
        if (parseFloat($("#totalDiscount").val()) < 90) {
            var r = confirm("是否确定修改开单并提交？");
            if (r == true) {
                $.ajax({
                    url: Feng.ctxPath + "/consultant/getCustomerStatus/" + $("#customerId").val() ,
                    dataType: "json",
                    contentType: "application/json",
                    type: "POST",
                    success: function (data) {
                        if (data.split(",")[0] == '复诊') {
                            alert("复诊不能重新开单,请去进行重咨操作！");
                            return;
                        } else if (data.split(",")[2] == 3) {
                            alert("患者TC不是当前登录人，请联系前台分配患者！");
                            return;
                        } else {
                            $.ajax({
                                url: Feng.ctxPath + "/consultant/updateDealInfo",
                                dataType: "json",
                                contentType: "application/json",
                                data: JSON.stringify(billList),
                                type: "POST",
                                success: function (data) {
                                    if (data.split(",")[0] == 55) {
                                        alert("前台只能开洁牙项目！");
                                    } else {
                                        if (data.split(",")[0] > 0) {
                                            var operationTime = data.split(",")[1];
                                            var index = layer.open({
                                                type: 2,
                                                title: '开单打印',
                                                area: ['60%', '100%'], //宽高
                                                fix: true, //不固定
                                                maxmin: true,
                                                content: Feng.ctxPath + '/consultant/singlePrint/' + customerId + "/" + operationTime,
                                                success: function () {
                                                    if(parent.$("#detail").val()=='isArrive') {
                                                        window.parent.location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + customerId + '/2/0'
                                                    }else{
                                                        window.parent.location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + customerId + '/3/0'
                                                    }
                                                }
                                            });
                                        } else if (data.split(",")[0] == -1) {
                                            layer.msg('操作失败！', {time: 1000, icon: 2});
                                            setTimeout(function () {
                                                $("#relaodBtn", parent.document).click();
                                                parent.layer.close(windowNameIndex);
                                            }, 1200);
                                            return false;
                                        } else if (data.split(",")[0] == -2) {
                                            layer.msg('已结账，无法修改！', {time: 1000, icon: 2});
                                            setTimeout(function () {
                                                $("#relaodBtn", parent.document).click();
                                                parent.layer.close(windowNameIndex);
                                            }, 1200);
                                            return false;
                                        } else if (data.split(",")[0] == -3) {
                                            layer.msg('操作失败！', {time: 1000, icon: 2});
                                            setTimeout(function () {
                                                $("#relaodBtn", parent.document).click();
                                                parent.layer.close(windowNameIndex);
                                            }, 1200);
                                            return false;
                                        }
                                    }
                                }
                            });
                        }
                    }
                });
            }
        } else {
            var r = confirm("是否确定修改开单并提交？");
            if (r == true) {
                $.ajax({
                    url: Feng.ctxPath + "/consultant/getCustomerStatus/" + $("#customerId").val() ,
                    dataType: "json",
                    contentType: "application/json",
                    type: "POST",
                    success: function (data) {
                        if (data.split(",")[0] == '复诊') {
                            alert("复诊不能重新开单,请去进行重咨操作！");
                            return;
                        } else if (data.split(",")[2] == 3) {
                            alert("患者TC不是当前登录人，请联系前台分配患者！");
                            return;
                        } else {
                            $.ajax({
                                url: Feng.ctxPath + "/consultant/updateDealInfo",
                                dataType: "json",
                                contentType: "application/json",
                                data: JSON.stringify(billList),
                                type: "POST",
                                success: function (data) {
                                    if (data.split(",")[0] == 55) {
                                        alert("前台只能开洁牙项目！");
                                    } else {
                                        if (data.split(",")[0] > 0) {
                                            var operationTime = data.split(",")[1];
                                            var index = layer.open({
                                                type: 2,
                                                title: '开单打印',
                                                area: ['60%', '100%'], //宽高
                                                fix: true, //不固定
                                                maxmin: true,
                                                content: Feng.ctxPath + '/consultant/singlePrint/' + customerId + "/" + operationTime,
                                                success: function () {
                                                    if(parent.$("#detail").val()=='isArrive') {
                                                        window.parent.location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + customerId + '/2/0'
                                                    }else{
                                                        window.parent.location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + customerId + '/3/0'
                                                    }
                                                }
                                            });
                                        } else if (data.split(",")[0] == -1) {
                                            layer.msg('操作失败！', {time: 1000, icon: 2});
                                            setTimeout(function () {
                                                $("#relaodBtn", parent.document).click();
                                                parent.layer.close(windowNameIndex);
                                            }, 1200);
                                            return false;
                                        } else if (data.split(",")[0] == -2) {
                                            layer.msg('已结账，无法修改！', {time: 1000, icon: 2});
                                            setTimeout(function () {
                                                $("#relaodBtn", parent.document).click();
                                                parent.layer.close(windowNameIndex);
                                            }, 1200);
                                            return false;
                                        } else if (data.split(",")[0] == -3) {
                                            layer.msg('操作失败！', {time: 1000, icon: 2});
                                            setTimeout(function () {
                                                $("#relaodBtn", parent.document).click();
                                                parent.layer.close(windowNameIndex);
                                            }, 1200);
                                            return false;
                                        }
                                    }
                                }
                            });
                        }
                    }
                });
            }
        }
    } else {//没有使用储值和返现抵扣
        var dataList = $(".proDealInfo");
        var billList = [];
        var newTotalSum = parseFloat($("#newTotalSum").val());
        var payable = parseFloat($("#newPayAmount").val());
        if (newTotalSum == payable) {
            for (var i = 0; i < dataList.length; i++) {
                var obj = dataList[i];
                var dealInfo = {};
                dealInfo.reservationsIdx = reservationsIdx;
                var treatmentUnit = $("#treatmentUnit").val();
                dealInfo.customerId = customerId;
                dealInfo.reservationsId = reservationsId;
                dealInfo.operationTime = operationTime;
                dealInfo.remarks = remarks;
                dealInfo.payServiceId = $(obj).find(".payServiceId").val();
                dealInfo.vipPayserviceId = $(obj).find(".vipPayserviceId").val();
                dealInfo.payServiceNum = parseFloat($(obj).find(".num").val());
                dealInfo.deductionType = 0;
                dealInfo.deductionAmount = 0;
                dealInfo.depositAmount = 0;
                var payServiceNum = parseFloat($(obj).find(".num").val());
                var doctor = $(obj).find(".doctor").val();
                var nurse = $("#projectNurse").val();
                if (payServiceNum < 1) {
                    layer.msg('请输入项目数量！', {time: 1000, icon: 2});
                    return false;
                }
                if ((doctor == null || doctor == "") && (nurse == null || nurse == "")) {
                    layer.msg('请选择医生或护士！', {time: 1000, icon: 2});
                    return false;
                }
                if ("洁牙" == treatmentUnit) {
                    dealInfo.dentalHygienist = $(obj).find(".doctor").val();
                } else {
                    dealInfo.dentalHygienist = "";
                }
                dealInfo.doctor = $(obj).find(".doctor").val();
                dealInfo.nurse = $("#projectNurse").val();
                dealInfo.orderDiscount = $(obj).find(".discount").val();
                dealInfo.leftUp = $(obj).find(".leftUp").html();
                dealInfo.leftDown = $(obj).find(".leftDown").html();
                dealInfo.rightUp = $(obj).find(".rightUp").html();
                dealInfo.rightDown = $(obj).find(".rightDown").html();
                dealInfo.id = $(obj).find(".id").val();
                dealInfo.foldingPay = parseFloat($(obj).find(".amount").val());
                dealInfo.serviceActualPay = parseFloat($(obj).find(".amount").val());
                dealInfo.deductionType = "0";
                dealInfo.deductionTypeName = "";
                dealInfo.deductionAmount = 0;
                dealInfo.depositAmount = 0;
                dealInfo.payService = parseFloat($(obj).find(".payService").val());     //折扣价
                dealInfo.discountHide = parseFloat($(obj).find(".discountHide").val());     //未打折单价
                dealInfo.primaryAmount = parseFloat($(obj).find(".primary").val());         //未打折总价
                dealInfo.wholeDiscount = $("#totalDiscount").val();
                dealInfo.arrears = 0;
                dealInfo.intamount = intamount;
                dealInfo.lowestPriceBill = lowestPriceBill;
                billList.push(dealInfo);
            }
        } else {
            for (var i = 0; i < dataList.length; i++) {
                var obj = dataList[i];
                var dealInfo = {};
                dealInfo.reservationsIdx = reservationsIdx;
                var treatmentUnit = $("#treatmentUnit").val();
                dealInfo.customerId = customerId;
                dealInfo.reservationsId = reservationsId;
                dealInfo.operationTime = operationTime;
                dealInfo.remarks = remarks;
                dealInfo.payServiceId = $(obj).find(".payServiceId").val();
                dealInfo.vipPayserviceId = $(obj).find(".vipPayserviceId").val();
                dealInfo.payServiceNum = parseFloat($(obj).find(".num").val());
                dealInfo.deductionType = 0;
                dealInfo.deductionAmount = 0;
                dealInfo.depositAmount = 0;
                var payServiceNum = parseFloat($(obj).find(".num").val());
                var doctor = $(obj).find(".doctor").val();
                var nurse = $("#projectNurse").val();
                if (payServiceNum < 1) {
                    layer.msg('请输入项目数量！', {time: 1000, icon: 2});
                    return false;
                }
                if ((doctor == null || doctor == "") && (nurse == null || nurse == "")) {
                    layer.msg('请选择医生或护士！', {time: 1000, icon: 2});
                    return false;
                }
                if ("洁牙" == treatmentUnit) {
                    dealInfo.dentalHygienist = $(obj).find(".doctor").val();
                } else {
                    dealInfo.dentalHygienist = "";
                }
                dealInfo.foldingPay = parseFloat($(obj).find(".amount").val());
                dealInfo.doctor = $(obj).find(".doctor").val();
                dealInfo.nurse = $("#projectNurse").val();
                dealInfo.orderDiscount = $(obj).find(".discount").val();
                dealInfo.leftUp = $(obj).find(".leftUp").html();
                dealInfo.leftDown = $(obj).find(".leftDown").html();
                dealInfo.rightUp = $(obj).find(".rightUp").html();
                dealInfo.rightDown = $(obj).find(".rightDown").html();
                dealInfo.id = $(obj).find(".id").val();
                dealInfo.deductionType = "0";
                dealInfo.deductionTypeName = "";
                dealInfo.deductionAmount = 0;
                dealInfo.depositAmount = 0;
                dealInfo.discountHide = parseFloat($(obj).find(".discountHide").val());     //折扣价
                dealInfo.payService = parseFloat($(obj).find(".payService").val());     //未打折单价
                dealInfo.primaryAmount = parseFloat($(obj).find(".primary").val());         //未打折总价
                dealInfo.wholeDiscount = $("#totalDiscount").val();
                var serviceActualPay = parseFloat($(obj).find(".amount").val());
                if (payable >= serviceActualPay) {
                    dealInfo.serviceActualPay = parseFloat($(obj).find(".amount").val());
                    dealInfo.arrears = 0;
                    payable = payable - serviceActualPay;
                } else {
                    dealInfo.serviceActualPay = payable;
                    dealInfo.arrears = serviceActualPay - payable;
                    payable = 0;
                }
                dealInfo.intamount = intamount;
                dealInfo.lowestPriceBill = lowestPriceBill;
                billList.push(dealInfo);
            }
        }
        if (parseFloat($("#totalDiscount").val()) < 90) {
            var r = confirm("是否确定修改开单并提交？");
            if (r == true) {
                $.ajax({
                    url: Feng.ctxPath + "/consultant/getCustomerStatus/" + $("#customerId").val() ,
                    dataType: "json",
                    contentType: "application/json",
                    type: "POST",
                    success: function (data) {
                        if (data.split(",")[0] == '复诊') {
                            alert("复诊不能重新开单,请去进行重咨操作！");
                            return;
                        } else if (data.split(",")[2] == 3) {
                            alert("患者TC不是当前登录人，请联系前台分配患者！");
                            return;
                        } else {
                            $.ajax({
                                url: Feng.ctxPath + "/consultant/updateDealInfo",
                                dataType: "json",
                                contentType: "application/json",
                                data: JSON.stringify(billList),
                                type: "POST",
                                success: function (data) {
                                    if (data.split(",")[0] == 55) {
                                        alert("前台只能开洁牙项目！");
                                    } else {
                                        if (data.split(",")[0] > 0) {
                                            var operationTime = data.split(",")[1];
                                            var index = layer.open({
                                                type: 2,
                                                title: '开单打印',
                                                area: ['60%', '100%'], //宽高
                                                fix: true, //不固定
                                                maxmin: true,
                                                content: Feng.ctxPath + '/consultant/singlePrint/' + customerId + "/" + operationTime,
                                                success: function () {
                                                    if(parent.$("#detail").val()=='isArrive') {
                                                        window.parent.location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + customerId + '/2/0'
                                                    }else{
                                                        window.parent.location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + customerId + '/3/0'
                                                    }
                                                }
                                            });
                                        } else if (data.split(",")[0] == -1) {
                                            layer.msg('操作失败！', {time: 1000, icon: 2});
                                            setTimeout(function () {
                                                $("#relaodBtn", parent.document).click();
                                                parent.layer.close(windowNameIndex);
                                            }, 1200);
                                            return false;
                                        } else if (data.split(",")[0] == -2) {
                                            layer.msg('已结账，无法修改！', {time: 1000, icon: 2});
                                            setTimeout(function () {
                                                $("#relaodBtn", parent.document).click();
                                                parent.layer.close(windowNameIndex);
                                            }, 1200);
                                            return false;
                                        } else if (data.split(",")[0] == -3) {
                                            layer.msg('操作失败！', {time: 1000, icon: 2});
                                            setTimeout(function () {
                                                $("#relaodBtn", parent.document).click();
                                                parent.layer.close(windowNameIndex);
                                            }, 1200);
                                            return false;
                                        }
                                    }
                                }
                            });
                        }
                    }
                });
            }
        } else {
            var r = confirm("是否确定修改开单并提交？");
            if (r == true) {
                $.ajax({
                    url: Feng.ctxPath + "/consultant/getCustomerStatus/" + $("#customerId").val() ,
                    dataType: "json",
                    contentType: "application/json",
                    type: "POST",
                    success: function (data) {
                        if (data.split(",")[0] == '复诊') {
                            alert("复诊不能重新开单,请去进行重咨操作！");
                            return;
                        } else if (data.split(",")[2] == 3) {
                            alert("患者TC不是当前登录人，请联系前台分配患者！");
                            return;
                        } else {
                            $.ajax({
                                url: Feng.ctxPath + "/consultant/updateDealInfo",
                                dataType: "json",
                                contentType: "application/json",
                                data: JSON.stringify(billList),
                                type: "POST",
                                success: function (data) {
                                    if (data.split(",")[0] == 55) {
                                        alert("前台只能开洁牙项目！");
                                    } else {
                                        if (data.split(",")[0] > 0) {
                                            var operationTime = data.split(",")[1];
                                            var index = layer.open({
                                                type: 2,
                                                title: '开单打印',
                                                area: ['60%', '100%'], //宽高
                                                fix: true, //不固定
                                                maxmin: true,
                                                content: Feng.ctxPath + '/consultant/singlePrint/' + customerId + "/" + operationTime,
                                                success: function () {
                                                    if(parent.$("#detail").val()=='isArrive') {
                                                        window.parent.location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + customerId + '/2/0'
                                                    }else{
                                                        window.parent.location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + customerId + '/3/0'
                                                    }
                                                }
                                            });
                                        } else if (data.split(",")[0] == -1) {
                                            layer.msg('操作失败！', {time: 1000, icon: 2});
                                            setTimeout(function () {
                                                $("#relaodBtn", parent.document).click();
                                                parent.layer.close(windowNameIndex);
                                            }, 1200);
                                            return false;
                                        } else if (data.split(",")[0] == -2) {
                                            layer.msg('已结账，无法修改！', {time: 1000, icon: 2});
                                            setTimeout(function () {
                                                $("#relaodBtn", parent.document).click();
                                                parent.layer.close(windowNameIndex);
                                            }, 1200);
                                            return false;
                                        } else if (data.split(",")[0] == -3) {
                                            layer.msg('操作失败！', {time: 1000, icon: 2});
                                            setTimeout(function () {
                                                $("#relaodBtn", parent.document).click();
                                                parent.layer.close(windowNameIndex);
                                            }, 1200);
                                            return false;
                                        }
                                    }
                                }
                            });
                        }
                    }
                });
            }
        }
    }
}

inputNum = function () {
    var input = document.getElementsByClassName('totleinput');
    var inputNum = 0;
    for (var i = 0; i < input.length; i++) {
        inputNum += parseFloat(input[i].value);
    }
    $("#serviceActualPay").attr("value", inputNum);
};
//变更科室
changeTreatmentUnit = function () {
    var treatmentUnit = $("#treatmentUnit").val();
    var customerId = $('#customerId').val();
    var tc = treatmentUnit + "," + customerId;
    var index = layer.open({
        type: 2,
        title: '变更科室',
        area: ['60%', '50%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultantTwo/change_treatmentUnit/' + tc
    });
    this.layerIndex = index;
};